import React from "react";
import Conditions from "./Conditions";
import type {
  NestFilterChangeHandler,
  NestFilterConditions,
  NestFilterFields,
} from "./type";
import NestFilterReveal from "./Reveal";

interface NestFilterProps {
  value?: NestFilterConditions;
  showError?: boolean; // TODO, 未完成
  disabled?: boolean;
  defaultFirstItem?: boolean; // 默认显示首项
  fields: NestFilterFields;
  onChange: (conds: NestFilterConditions) => void;
}

function NestFilter({
  fields,
  value = [],
  showError,
  disabled,
  defaultFirstItem,
  onChange,
}: NestFilterProps) {
  const handler: NestFilterChangeHandler = (seq, joint, conds) => {
    // root 节点的 seq 和 joint 是无用的
    onChange(conds);
  };

  return (
    <Conditions
      showError={showError}
      seq={-1}
      joint="AND"
      fields={fields}
      conditions={value}
      onChange={handler}
      isRoot
      defaultFirstItem={defaultFirstItem}
      disabled={disabled}
    />
  );
}

NestFilter.Reveal = NestFilterReveal;

export default NestFilter;
